#ifndef SC_ADB_TUNNEL_H
#define SC_ADB_TUNNEL_H

#include "common.h"

#include <stdbool.h>
#include <stdint.h>

#include "options.h"
#include "util/intr.h"
#include "util/net.h"

/*
adb reverse tcp:8000[手机端口] tcp:9999[PC端口]
手机访问自身的8000端口的流量都会转发到PC的9999端口。就是建立一个反向代理。

adb forward的功能是建立一个转发，
forward本身就是转发和正向代理的意思。
adb forward tcp:10000[PC端口] tcp:8000[手机端口]
PC无法直接访问手机的端口 （类比国内无法访问外网），那么通PC的10000端口代理手机的8000端口就可以了
adb forward的功能就是让 PC能够访问手机端口。即代理手机流量
*/

// 通道上下文
struct sc_adb_tunnel {
    
    bool enabled;
    
    bool forward; // use "adb forward" instead of "adb reverse"
    
    sc_socket server_socket; // only used if !forward
    
    uint16_t local_port;
};

/**
 * Initialize the adb tunnel struct to default values
 * 用默认值初始化adb通道的结构数据
 * @param tunnel 通道上下文
 */
void
sc_adb_tunnel_init(struct sc_adb_tunnel *tunnel);

/**
 * Open a tunnel
 *
 * Blocking calls may be interrupted asynchronously via `intr`.
 *
 * If `force_adb_forward` is not set, then attempts to set up an "adb reverse"
 * tunnel first. Only if it fails (typical on old Android version connected via
 * TCP/IP), use "adb forward".
 */
bool
sc_adb_tunnel_open(struct sc_adb_tunnel *tunnel, struct sc_intr *intr,
                   const char *serial, const char *device_socket_name,
                   struct sc_port_range port_range, bool force_adb_forward);

/**
 * Close the tunnel
 */
bool
sc_adb_tunnel_close(struct sc_adb_tunnel *tunnel, struct sc_intr *intr,
                    const char *serial, const char *device_socket_name);

#endif
